package cn.edu.ecut.se2024.Service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import cn.edu.ecut.se2024.User.Admin;

public class adminservice {
	public List<Admin> getAllAdmins() {
        List<Admin> list = new ArrayList<>();
        String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=Stock;encrypt=true;trustServerCertificate=true";
        String userName = "sa";
        String userPwd = "123456";
        String sql = "select * from Admin";

        try (Connection con = DriverManager.getConnection(dbURL, userName, userPwd);
             Statement st = con.createStatement();
             ResultSet rs = st.executeQuery(sql)) {

            System.out.println("数据库连接成功");

            while (rs.next()) {
                Admin admin = new Admin();
                admin.setName(rs.getString("name"));
                admin.setGender(rs.getString("gender"));
                admin.setJobnumber(rs.getString("jobnumber"));
                admin.setPassword(rs.getString("password"));
                list.add(admin);
            }
        } catch (SQLException e) {
            // 输出异常信息或者记录日志
            e.printStackTrace();
            throw new RuntimeException("数据库访问异常", e);
        }

        return list;
    }
}